package com.gujiangbo.redemo;

import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;

public class JedisPoolTest {

    private JedisPool jedisPool = JedisFactory.getJedisPool();
    private Jedis jedis = JedisFactory.getJedis();
    private Pipeline pipeline = JedisFactory.getPipeLine();

    @Test
    public void testPool() {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(0);
            String s = jedis.get("name");
            System.out.println(s);
            // Thread.sleep(30000000);

        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            if (jedis != null) {
                //jedis.close()；close操作不是关闭连接，而是归还连接池
                jedis.close();
            }
        }
    }

    @Test
    public void testPipeLine() throws InterruptedException {
        //1.创建PipeLine 流水线对象
        //Pipeline pipeLine = JedisFactory.getPipeLine();
        // 2.pipeLine 执行命令，注意此时命令没有真正的执行
        for (int i = 100; i < 300; i++) {
            String str = "str" + i;
            String s;
            if ((s = jedis.get(str)) != null) {
                System.out.println("i=" + i + "---->" + s);
                pipeline.del(str);
            }
        }
        // 3.执行命令
        pipeline.sync();
    }
}
